
import java.io.InputStream;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.m2g.SVGAnimator;
import javax.microedition.m2g.SVGEventListener;
import javax.microedition.m2g.SVGImage;
import javax.microedition.m2g.ScalableGraphics;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

import org.w3c.dom.Document;
import org.w3c.dom.svg.SVGElement;
import org.w3c.dom.svg.SVGSVGElement;


public class SVGAnimatorMidlet extends MIDlet implements CommandListener{

	private Canvas canvas;
	private SVGImage svgImage = null;
	private SVGAnimator svgAnimator;

	private static double x = 660000.6;
	private static double y = -7571310.4 ;

	public SVGAnimatorMidlet(){

		try{
			InputStream svgStream = getClass().getResourceAsStream("/bairros_cianela.svg");
			if(svgStream == null){
				throw new Error("N�o foi poss�vel carregar helloworld.svg");
			}
			svgImage = (SVGImage)SVGImage.createImage(svgStream, null);


//			svgImage.activate();

			svgAnimator = SVGAnimator.createAnimator(svgImage);
			svgAnimator.setSVGEventListener(new MySvgEventListener());

//			canvas = new SVGImageCanvas(svgImage);

			canvas = (Canvas)(svgAnimator.getTargetComponent());

			System.out.println("Height..." + canvas.getHeight());
			System.out.println("Width..." + canvas.getWidth());

			Display.getDisplay(this).setCurrent(canvas);

			canvas.setCommandListener(this);

			svgAnimator.play();

		}catch(Exception e){
			e.printStackTrace();
			System.out.println("Problemas no construtor SVGAnimatorMidlet()...");
		}
	}
	protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
		// TODO Auto-generated method stub

	}

	protected void pauseApp() {
		// TODO Auto-generated method stub

	}

	protected void startApp() throws MIDletStateChangeException {
		// TODO Auto-generated method stub
//		svgAnimator.play();
//		Display.getDisplay(this).setCurrent(canvas);
	}

	public void commandAction(Command c, Displayable s){

	}

	public class MySvgEventListener implements SVGEventListener{

		public MySvgEventListener(){
//			System.out.println("Entrei no MySvgEventListener()....");
//			int gameAction = canvas.getGameAction( keyCode );
//			switch ( gameAction )
//			{
//			case Canvas.UP:
//			scale( 0.10f );
//			break;
//			case Canvas.LEFT:
//			rotate( -10.0f );
//			break;
//			case Canvas.DOWN:
//			scale( -0.10f );
//			break;
//			case Canvas.RIGHT:
//			rotate( 10 );
//			break;
//			default:
//			// leave unchanged
//			}
		}

		private void rotate( final float delta )
		{
			// put ourselves on the animator's thread
			svgAnimator.invokeLater( new Runnable()
			{
				public void run()
				{
					// execute the transformation
					Document document = svgImage.getDocument();
					SVGSVGElement root = (SVGSVGElement)
					document.getDocumentElement();
					root.setCurrentRotate( root.getCurrentRotate() + delta );
				}
			} );
		}

		private void scale( final float delta )
		{
			// put ourselves on the animator's thread
			svgAnimator.invokeLater( new Runnable()
			{
				public void run()
				{
					// execute the transformation
					Document document = svgImage.getDocument();

//					SVGElement text = (SVGElement)document.getElementById("bairros");
//					text.setTrait("transform", "translate(640000.6 -7581310.4)");

					System.out.println("Transform...");

					SVGSVGElement root = (SVGSVGElement)
					document.getDocumentElement();
					root.setCurrentScale( root.getCurrentScale() + delta );
					System.out.println("Transla��o:...." + root.getBBox() + " " + root.getScreenBBox() + " " + root.getScreenCTM());
				}
			} );
		}

		private void arrastpos( final float delta )
		{
			// put ourselves on the animator's thread
			svgAnimator.invokeLater( new Runnable()
			{
				public void run()
				{
					// execute the transformation
					Document document = svgImage.getDocument();

					SVGElement text = (SVGElement)document.getElementById("bairros");
					x = x + delta;
					text.setTrait("transform", "translate(" + x + " " + y + ")");
					System.out.println("transform..." + text.getTrait("transform"));
				}
			} );

		}

		private void arrastneg( final float delta )
		{
			// put ourselves on the animator's thread
			svgAnimator.invokeLater( new Runnable()
			{
				public void run()
				{
					// execute the transformation
					Document document = svgImage.getDocument();

					SVGElement text = (SVGElement)document.getElementById("bairros");
					x = x - delta;
					System.out.println("transform..." + text.getTrait("transform"));
					text.setTrait("transform", "translate(" + x + " " + y + ")");
				}
			} );

		}

		public void hideNotify() {
			// TODO Auto-generated method stub

		}

		public void keyPressed(int keyCode) {
			// TODO Auto-generated method stub
			int gameAction = canvas.getGameAction( keyCode );
			switch ( gameAction )
			{
			case Canvas.UP:
				scale( 0.10f );
				break;
			case Canvas.LEFT:
//				rotate( -10.0f );
				arrastneg(1000f);
				break;
			case Canvas.DOWN:
				scale( -0.10f );
				break;
			case Canvas.RIGHT:
//				rotate( 10 );
				arrastpos(1000f);
				break;
			default:
				// leave unchanged
			}
		}

		public void keyReleased(int arg0) {
			// TODO Auto-generated method stub

		}

		public void pointerPressed(int x, int y) {
			// TODO Auto-generated method stub
			SVGAnimatorMidlet.x = x;
			SVGAnimatorMidlet.y = y;
			svgImage.activate();
			svgImage.dispatchMouseEvent("click", x, y);
			System.out.println("X: " + x);
			System.out.println("Y: " + y);
		}

		public void pointerReleased(int arg0, int arg1) {
			// TODO Auto-generated method stub

		}

		public void showNotify() {
			// TODO Auto-generated method stub

		}

		public void sizeChanged(int arg0, int arg1) {
			// TODO Auto-generated method stub

		}

	}

	class SVGImageCanvas extends Canvas {
		/**
		 * The SVGImage painted by the canvas.
		 */
		protected SVGImage svgImage;

		/**
		 * The ScalableGraphics used to paint into the midp
		 * Graphics instance.
		 */
		protected ScalableGraphics sg =
			ScalableGraphics.createInstance();

		/**
		 * @param svgImage the SVGImage
		 * this canvas should paint.
		 */
		protected SVGImageCanvas(final SVGImage svgImage) {
			this.svgImage = svgImage;
		}

		public void paint(Graphics g) {
			g.setColor(255, 255, 255);
			g.fillRect(0, 0, getWidth(), getHeight());

			g.drawString(x + "/" + y, 0, 0,
					Graphics.TOP|Graphics.LEFT);

			sg.bindTarget(g);

			System.out.println("Antes Height..." + svgImage.getViewportHeight());
			System.out.println("Antes Width..." + svgImage.getViewportWidth());

			svgImage.setViewportWidth(getWidth());
			svgImage.setViewportHeight(getHeight());

			System.out.println("Depois Height..." + svgImage.getViewportHeight());
			System.out.println("Depois Width..." + svgImage.getViewportWidth());

			sg.render(0, 0, svgImage);
			sg.releaseTarget();
		}
	}

}